﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataSync.DAL;
using DataSync.Model;

namespace DataSync.BLL
{
    /// <summary>
    /// 自定义同步参数结构
    /// </summary>
    public struct CustomizeSyncPara
    {
        public SyncBase obj;
        public DateTime startDate;
        public DateTime endDate;
    };

    public class CustomizeSyncService
    {
        private static ListBox lbMsg;

        public CustomizeSyncService()
        {

        }

        public ListBox LbMsg
        {
            get { return lbMsg; }
            set { lbMsg = value; }
        }

        /// <summary>
        /// 处理所有同步数据
        /// </summary>
        /// <returns></returns>
        public static void ProcessData(object para)
        {
            SyncManage syncManage = new SyncManage();
            Sync dataSync = new Sync();
            SyncLog syncLog = new SyncLog();
            syncManage.LbMsg = CustomizeSyncService.lbMsg;
            CustomizeSyncPara threadPara = (CustomizeSyncPara)para;
            threadPara.obj.IsCustomerSync = true;
            string sConnString = System.Configuration.ConfigurationManager.ConnectionStrings["sConnString"].ConnectionString;
            string tConnString = System.Configuration.ConfigurationManager.ConnectionStrings["tConnString"].ConnectionString;
            syncManage.ClearMsg();

            try
            {
                using (SQLHelper sHelper = new SQLHelper(sConnString))
                {
                    using (SQLHelper tHelper = new SQLHelper(tConnString))
                    {
                        syncManage.LbMsg = lbMsg;

                        switch (threadPara.obj.SyncType)
                        {
                            case 1:
                                if (threadPara.obj.SourceTableName == "ACCOUNT_MONTH")
                                {
                                    threadPara.startDate = threadPara.startDate.AddDays(1);
                                    DateTime dt = threadPara.startDate.AddDays(1 - threadPara.startDate.Day);
                                    //循环用户选择的时间段
                                    while (dt <= threadPara.endDate)
                                    {
                                        //IncrementDataSyncByMonth方法里 要把dt的月份-1，所以这里+1
                                        if (threadPara.obj.IsSubTable)
                                        {
                                            threadPara.obj.TargetTableName = threadPara.obj.TargetTableName.Substring(0, threadPara.obj.TargetTableName.LastIndexOf("_") + 1);
                                            threadPara.obj.TargetTableName += dt.Month;
                                        }
                                        syncManage.IncrementDataSyncByMonth(threadPara.obj, sHelper, tHelper, dataSync, syncLog, null, dt.AddMonths(1));
                                        dt = dt.AddMonths(1);
                                    }

                                }
                                else
                                {
                                    syncManage.IncrementDataSyncByDate(threadPara.obj, sHelper, tHelper, dataSync, syncLog, threadPara.startDate, threadPara.endDate);
                                }

                                break;
                            case 0:
                                syncManage.FullDataSync(threadPara.obj, sHelper, tHelper, dataSync, syncLog);
                                break;
                            default:
                                break;
                        }
                    }
                }
                if (dataSync.IsAllSuccess())
                {
                    syncManage.UpdateFinishTime();
                }
            }
            catch (Exception ex)
            {
                syncManage.ShowSyncMsg(ex);
                syncManage.Log.Error(ex);
                TraceHelper.WriteLine(ex);
            }
        }
    }
}
